package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.ExamAnswer;
import com.ruoyi.system.domain.ExamQuestion;
import com.ruoyi.system.mapper.ExamAnswerMapper;
import com.ruoyi.system.mapper.ExamQuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ExamPaperMapper;
import com.ruoyi.system.domain.ExamPaper;
import com.ruoyi.system.service.IExamPaperService;

/**
 * 试卷Service业务层处理
 *
 * @author tyro
 * @date 2025-05-21
 */
@Service
public class ExamPaperServiceImpl implements IExamPaperService
{
    @Autowired
    private ExamPaperMapper examPaperMapper;
    @Autowired
    private ExamQuestionMapper examQuestionMapper;
    @Autowired
    private ExamAnswerMapper examAnswerMapper;

    /**
     * 查询试卷
     *
     * @param id 试卷主键
     * @return 试卷
     */
    @Override
    public ExamPaper selectExamPaperById(Long id)
    {
        return examPaperMapper.selectExamPaperById(id);
    }

    /**
     * 查询试卷列表
     *
     * @param examPaper 试卷
     * @return 试卷
     */
    @Override
    public List<ExamPaper> selectExamPaperList(ExamPaper examPaper)
    {
        return examPaperMapper.selectExamPaperList(examPaper);
    }

    /**
     * 新增试卷
     *
     * @param examPaper 试卷
     * @return 结果
     */
    @Override
    public int insertExamPaper(ExamPaper examPaper)
    {
        examPaper.setCreateTime(DateUtils.getNowDate());
        return examPaperMapper.insertExamPaper(examPaper);
    }

    /**
     * 修改试卷
     *
     * @param examPaper 试卷
     * @return 结果
     */
    @Override
    public int updateExamPaper(ExamPaper examPaper)
    {
        return examPaperMapper.updateExamPaper(examPaper);
    }

    /**
     * 批量删除试卷
     *
     * @param ids 需要删除的试卷主键
     * @return 结果
     */
    @Override
    public int deleteExamPaperByIds(Long[] ids)
    {
        // 创建问题ID列表(使用List而不是数组以便动态增长)
        List<Long> questionIdList = new ArrayList<>();
        // 查询所有试卷下的所有题目
        for (Long paperId : ids) {
            ExamQuestion examQuestion = new ExamQuestion();
            examQuestion.setPaperId(paperId);
            List<ExamQuestion> examQuestionList = examQuestionMapper.selectExamQuestionList(examQuestion);
            // 将所有问题ID添加到列表
            if (examQuestionList != null && !examQuestionList.isEmpty()) {
                for (ExamQuestion question : examQuestionList) {
                    questionIdList.add(question.getId());
                }
            }
        }
        // 如果有问题需要删除
        if (!questionIdList.isEmpty()) {
            // 将List转换回数组
            Long[] questionIds = questionIdList.toArray(new Long[0]);
            // 删除问题对应的答案
            examAnswerMapper.deleteExamAnswerByQuestionIds(questionIds);
        }
        // 删除试卷中的问题
        examQuestionMapper.deleteExamQuestionByPaperIds(ids);
        // 删除试卷
        return examPaperMapper.deleteExamPaperByIds(ids);
    }


    /**
     * 删除试卷信息
     *
     * @param id 试卷主键
     * @return 结果
     */
    @Override
    public int deleteExamPaperById(Long id)
    {
        return examPaperMapper.deleteExamPaperById(id);
    }

    @Override
    public List<ExamPaper> selectExamPaperListByCourseId(Long courseId) {
        return examPaperMapper.selectExamPaperListByCourseId(courseId);
    }
}
